import { _decorator, Component, math, Node, PhysicsSystem2D, Vec2 } from 'cc';
import { playerState } from './playerState';
import { ENTITY_STATE_ENUM } from '../../Enum';
import { InputManager } from '../../Base/InputManager';
const { ccclass, property } = _decorator;

@ccclass('SlideState')
export class SlideState extends playerState {
    private isLeftWall: boolean = true;
    onEnter(): void {
        super.onEnter();
        this.fsm.lastOnWallLeftTime > 0 ? this.isLeftWall = true : this.isLeftWall = false;
        this.fsm.setGravityScale(0);
        // console.log('SlideState',this.fsm.player.rb.gravityScale );
    }
    update(deltaTime: number): void {
        super.update(deltaTime); 

        const speedDif = this.fsm.player.data.slideSpeed - this.rb.linearVelocity.y;
        let movement = speedDif * this.fsm.player.data.slideAccel;
        movement = math.clamp(
            movement,
            -Math.abs(speedDif) * (1 / PhysicsSystem2D.instance.fixedTimeStep),
            Math.abs(speedDif) * (1 / PhysicsSystem2D.instance.fixedTimeStep)
        );
        this.rb.applyForceToCenter(new Vec2(0, movement), true);

        
        // console.info("test :",this.fsm.lastOnWallLeftTime+"|||"+this.fsm.lastOnWallRightTime);
        if ( this.fsm.lastOnGroundTime > 0 ||
            (this.fsm.lastOnWallRightTime < 0 && this.fsm.lastOnWallLeftTime < 0)||
            (this.isLeftWall && InputManager.Instance.moveDir.x > 0)||
            (!this.isLeftWall && InputManager.Instance.moveDir.x < 0))
        {
            this.fsm.transit(ENTITY_STATE_ENUM.IDLE);
        }

        if(this.fsm.lastPressedJumpTime > 0){
            this.fsm.transit(ENTITY_STATE_ENUM.WALL_JUMP)
        }
                    
    }
    onExit(): void {
        super.onExit(); 
    }
}


